package com.ruoyi.ldzlsc.controller;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.ldzlsc.listener.QualityCheckResultListener;
import com.ruoyi.ldzlsc.service.WebSocketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * 测试控制器
 * 用于测试WebSocket和质检流程
 * 
 * @author ldzl
 * @date 2025-01-23
 */
@RestController
@RequestMapping("/test")
public class TestController extends BaseController {

    @Autowired
    private WebSocketService webSocketService;
    
    @Autowired
    private QualityCheckResultListener qualityCheckResultListener;

    /**
     * 测试WebSocket连接数
     */
    @GetMapping("/websocket/connections")
    public AjaxResult getWebSocketConnections() {
        int count = webSocketService.getConnectionCount();
        return AjaxResult.success("当前WebSocket连接数: " + count, count);
    }

    /**
     * 测试推送任务状态更新
     */
    @PostMapping("/websocket/push-task-status")
    public AjaxResult pushTaskStatus(@RequestParam Long taskId, 
                                   @RequestParam String oldStatus, 
                                   @RequestParam String newStatus, 
                                   @RequestParam String message) {
        webSocketService.pushTaskStatusUpdate(taskId, oldStatus, newStatus, message);
        return AjaxResult.success("任务状态更新推送成功");
    }

    /**
     * 测试推送质检进度
     */
    @PostMapping("/websocket/push-quality-progress")
    public AjaxResult pushQualityProgress(@RequestParam Long taskId, 
                                        @RequestParam(required = false) Long qualityCheckId, 
                                        @RequestParam String progress, 
                                        @RequestParam String message) {
        webSocketService.pushQualityCheckProgress(taskId, qualityCheckId, progress, message);
        return AjaxResult.success("质检进度推送成功");
    }

    /**
     * 测试推送质检完成
     */
    @PostMapping("/websocket/push-quality-complete")
    public AjaxResult pushQualityComplete(@RequestParam Long taskId, 
                                        @RequestParam(required = false) Long qualityCheckId, 
                                        @RequestParam String result, 
                                        @RequestParam String message) {
        webSocketService.pushQualityCheckComplete(taskId, qualityCheckId, result, message);
        return AjaxResult.success("质检完成推送成功");
    }

    /**
     * 测试推送入库完成
     */
    @PostMapping("/websocket/push-inbound-complete")
    public AjaxResult pushInboundComplete(@RequestParam Long taskId, 
                                        @RequestParam(required = false) Long inboundId, 
                                        @RequestParam String quantity, 
                                        @RequestParam String message) {
        webSocketService.pushInboundComplete(taskId, inboundId, quantity, message);
        return AjaxResult.success("入库完成推送成功");
    }

    /**
     * 模拟质检完成事件
     */
    @PostMapping("/quality-check/simulate-complete")
    public AjaxResult simulateQualityCheckComplete(@RequestParam Long taskId, 
                                                 @RequestParam String checkResult, 
                                                 @RequestParam BigDecimal goodQty) {
        qualityCheckResultListener.simulateQualityCheckComplete(taskId, checkResult, goodQty);
        return AjaxResult.success("质检完成事件模拟启动");
    }

    /**
     * 测试完整流程
     * 模拟从报工到质检完成的完整流程
     */
    @PostMapping("/workflow/simulate-complete")
    public AjaxResult simulateCompleteWorkflow(@RequestParam Long taskId, 
                                             @RequestParam(defaultValue = "10") BigDecimal goodQty,
                                             @RequestParam(defaultValue = "1") String finalResult) {
        
        new Thread(() -> {
            try {
                // 1. 模拟报工完成，状态变为待质检
                Thread.sleep(1000);
                webSocketService.pushTaskStatusUpdate(taskId, "2", "5", "报工完成，等待质检处理");
                
                // 2. 模拟质检单创建
                Thread.sleep(2000);
                webSocketService.pushQualityCheckProgress(taskId, null, "created", "质检单已创建，等待检验");
                
                // 3. 模拟质检进行中
                Thread.sleep(3000);
                webSocketService.pushQualityCheckProgress(taskId, null, "in_progress", "质检正在进行中...");
                
                // 4. 模拟质检完成
                Thread.sleep(2000);
                webSocketService.pushQualityCheckComplete(taskId, null, finalResult, 
                    "质检完成，结果: " + ("1".equals(finalResult) ? "合格" : "不合格"));
                
                // 5. 如果合格，模拟入库完成
                if ("1".equals(finalResult)) {
                    Thread.sleep(1000);
                    webSocketService.pushTaskStatusUpdate(taskId, "5", "3", "质检合格，任务完成");
                    
                    Thread.sleep(1000);
                    webSocketService.pushInboundComplete(taskId, null, goodQty.toString(), "产品入库完成");
                } else {
                    Thread.sleep(1000);
                    webSocketService.pushTaskStatusUpdate(taskId, "5", "4", "质检不合格，任务暂停");
                }
                
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }).start();
        
        return AjaxResult.success("完整流程模拟已启动，请观察WebSocket推送消息");
    }

    /**
     * 获取WebSocket连接状态信息
     */
    @GetMapping("/websocket/status")
    public AjaxResult getWebSocketStatus() {
        int connectionCount = webSocketService.getConnectionCount();
        
        return AjaxResult.success("WebSocket状态信息")
            .put("connectionCount", connectionCount)
            .put("status", connectionCount > 0 ? "有连接" : "无连接");
    }
}
